package cn.xyafu.lost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.xyafu.lost.R;
import cn.xyafu.lost.pojo.Comment;

public class CommentBodyAdapter extends BaseAdapter {
    private final Context context;  // 上下文
    private final List<Comment> comments; //数据源

    public CommentBodyAdapter(Context context, List<Comment> comments) {
        this.context = context;
        this.comments = comments;
    }

    @Override
    public int getCount() {
        return comments==null?0:comments.size();
    }

    @Override
    public Object getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Comment comment = (Comment) getItem(position);
        CommentBodyAdapter.ViewHolder viewHolder = null;
        if(convertView == null){ // 没有加载过,那么就创建
            viewHolder = new CommentBodyAdapter.ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.comment_item_layout,parent,false);
            viewHolder.comment_item_username =  convertView.findViewById(R.id.comment_item_username);
            viewHolder.comment_item_id =  convertView.findViewById(R.id.comment_item_id);
            viewHolder.comment_item_content = convertView.findViewById(R.id.comment_item_content);
            viewHolder.comment_item_time = convertView.findViewById(R.id.comment_item_time);
            convertView.setTag(viewHolder);

        }else{//加载过,那么就获取之前的
            viewHolder = (CommentBodyAdapter.ViewHolder) convertView.getTag();
        }
        // (3). 给组件添加数据
        viewHolder.comment_item_username.setText(comment.getUser().getUnikename());
        viewHolder.comment_item_id.setText(String.valueOf(comment.getUser().getUid()));
        viewHolder.comment_item_content.setText(comment.getCcontent());
        viewHolder.comment_item_time.setText(comment.getCdate());

        return convertView;

    }

    class ViewHolder {

        TextView comment_item_username;
        TextView comment_item_id;
        TextView comment_item_content;
        TextView comment_item_time;

    }
}
